package com.iamk.feature;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.iamk.util.GetLabel;

public class Features_v1 {
	BufferedImage bufferedImage;
	File folderImages;
	File folderMask;
	File fileFeatures;

	public Features_v1(String pathFolder, String pathFolderMask,
			String pathFeatures) {
		// TODO Auto-generated constructor stub
		bufferedImage = null;
		folderImages = new File(pathFolder);
		fileFeatures = new File(pathFeatures);
		folderMask = new File(pathFolderMask);
	}

	public void features() {
		NumberFormat formatter = new DecimalFormat("#.########");
		BufferedWriter bw = null;
		FColor fColor = null;
		ArrayList<float[]> result;
		String fileName = null;
		String strlabel = null;
		ArrayList<String[]> listLabel = GetLabel.readLabelFile("C:\\Users\\sev_user\\Desktop\\Data\\label\\labels-train.txt");
		try {
			bw = new BufferedWriter(new FileWriter(fileFeatures + ".arff"));
			// Writer header file arff
			bw.write("@RELATION feature\n");
			for (int i = 0; i < 9; i++) {
				bw.write("@ATTRIBUTE f" + (i + 1) + "	REAL\n");
			}
			bw.write("@attribute class {1,2,3,5,6,8,9,10,11,12,13,15,16,17}\n");
			bw.write("@data\n");
			int count=0;
			int idImages=1;
			if (folderImages.isDirectory() && folderImages.exists()
					&& folderMask.isDirectory() && folderMask.exists()) {
				for (int i = 0; i < folderImages.listFiles().length; i++) {
					fileName = folderImages.listFiles()[i].getName().substring(0,folderImages.listFiles()[i].getName().length() - 4);
//					 System.out.println(fileName);
					bufferedImage = ImageIO.read(folderImages.listFiles()[i]);
					if (bufferedImage.getWidth() > 8
							&& bufferedImage.getHeight() > 8) {
						fColor = new FColor(bufferedImage,folderMask.listFiles()[i].getPath(),0);
//						 System.out.println(folderImages.listFiles()[i].getName()+" "+folderMask.listFiles()[i].getName());
//						 if(!folderImages.listFiles()[i].getName().equals(folderMask.listFiles()[i].getName().subSequence(0, folderMask.listFiles()[i].getName().length()-4))){
//							 System.out.println(false);
//							 
//						 }
						result = fColor.extract();
						for (int z = 0; z < result.size(); z++) {
							// bw.write(fileName + ",");

							for (String[] label : listLabel) {
//								
								if (label[0].equals(fileName)&&Integer.parseInt(label[1]) == z + 1) {
									strlabel = "" + label[2];
//									 System.out.println(label[0] +" "+fileName);
									 count++;
									break;
								} else {
									strlabel = null;
								}
							}
							int l=0;
							if(strlabel!=null){
								l=Integer.parseInt((strlabel));
							}
							// l=1,2,3,5,6,8,9,10,11,12,13,15,16,17
							if(strlabel!=null&&l!=4&&l!=7&&l!=14){
								bw.write(idImages+","+(z+1)+",");
							for (int k = 0; k < result.get(z).length; k++) {
								// System.out.println(result.get(z)[k]);
								bw.write(formatter.format(result.get(z)[k])+",");
							}
							// Label;
									bw.write(strlabel + "\n");
							// bw.write("\n");
							}
						}
						idImages++;
						// String label = folderImages.listFiles()[i].getName()
						// .split("_")[2];
						// bw.write(label.substring(0, label.length() - 4));
						// bw.write(f.getName());
					}
					// System.out.println(f.getPath());
				}
			}
			System.out.println(count);
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
